package object.obj2D.explode;

import javax.microedition.khronos.opengles.GL10;

import media.sound.GameSound;

import object.obj2D.graphics.AnimSystem;
import object.obj2D.graphics.Square;
import system.GameOption;
import system.Graphics;
import object.obj3D.Object3D;

public class Explode {
	private GL10 gl;
	
	private AnimSystem m_explode = new AnimSystem("/res/raw/explode.bin", 0, 0);
	private Square m_bulletSquare = new Square(4f, 4f);
	
	private float m_alphaShooting;
	private float m_betaShooting;
	
	private boolean isDisplay = false;
	
	private Object3D m_character = null;

	private float m_fDistance;
	
	public Explode()
	{
		gl = Graphics.GetGraphics();
	}
	
	public void SetAlpha(float alpha)
	{
		m_alphaShooting = alpha;
	}
	
	public void SetBeta(float beta)
	{
		m_betaShooting = beta;
	}
	
	public void SetDistance(float distance)
	{
		m_fDistance = distance;
	}
	
	public float GetAlpha()
	{
		return m_alphaShooting;
	}
	
	public float GetBeta()
	{
		return m_betaShooting;
	}
	
	public float GetDistance()
	{
		return m_fDistance;
	}
	
	public void Update()
	{
		if(isDisplay)
		{
			if(!m_explode.isAnimPlaying())
			{
				m_explode.Stop();
				isDisplay = false;
			}

			if(m_character != null && m_explode.getPlayFrame() == 4)
			{
				m_character.Stop();
				if(GameOption.isSoundOn) GameSound.GetEffSound().PlayProtossDie();
			}
		}
	}
	
	public void Start()
	{
		isDisplay = true;
		m_explode.Play();
	}
	
	public boolean isPlaying()
	{
		return isDisplay;
	}
	
	public void SetCharacter(Object3D character)
	{
		m_character = character;
	}
	
	public void Render()
	{
		if(isDisplay)
		{
			gl.glDisable(GL10.GL_DEPTH_TEST);
			gl.glMatrixMode(GL10.GL_MODELVIEW);			
			gl.glPushMatrix();
			gl.glRotatef(m_betaShooting, 0, 1.0f, 0);
			gl.glRotatef(m_alphaShooting, 1.0f, 0, 0);
			gl.glTranslatef(0, 0, -m_fDistance);
			m_explode.Render();
			m_bulletSquare.Render();
			gl.glPopMatrix();
			gl.glEnable(GL10.GL_DEPTH_TEST);
		}
	}
}
